<?php
		
	/**
	 * 登记债权转让
	 * @param int $transfer_id  转让id
	 * @param int $t_user_id  受让用户ID
	 * @param int $MerCode  商户ID
	 * @param string $cert_md5 
	 * @param string $post_url
	 * @return string
	 */
	function RegisterCretansfer($transfer_id,$t_user_id, $platformNo,$post_url,$mchnt_user){
	
		$transfer = $GLOBALS['db']->getRow("select * from ".DB_PREFIX."deal_load_transfer where id = ".$transfer_id);
		$deal_id = intval($transfer['deal_id']);
		$user_id = intval($transfer['user_id']);	
		
		$user = get_user_info("*","id = ".$user_id);
		$tuser = get_user_info("*","id = ".$t_user_id);
		$user_load_transfer_fee = $GLOBALS['db']->getOne("SELECT user_load_transfer_fee FROM ".DB_PREFIX."deal WHERE id=".$deal_id);

		if (empty($user['ips_acct_no']) || empty($tuser['ips_acct_no'])){
			$result['info'] = '有一方未申请 托管 帐户';
		}
		
		
		$sql = "update ".DB_PREFIX."deal_load_transfer set lock_user_id = ".$t_user_id.", lock_time =".TIME_UTC;
		$sql .= " where ips_status = 0 and t_user_id = 0 and status = 1 and (lock_user_id = 0 || lock_user_id =".$t_user_id." || (lock_user_id > 0 && lock_time < ".(TIME_UTC - 600)."))";
		$sql .= " and id = ".$transfer_id;
		//echo $sql; exit;
		$GLOBALS['db']->query($sql);
		//转让锁定
		if ($GLOBALS['db']->affected_rows()){
			require_once APP_ROOT_PATH.'system/collocation/fuioupay/transferBu.php';
			
			require_once APP_ROOT_PATH."system/libs/user.php";
			
			$amt = round($transfer['transfer_amount'],2);//	转账金额	是
			$fee = round($transfer['transfer_amount']*$user_load_transfer_fee*0.01,2);//	手续费	是	该费用将会从指定费用方账户收取到平台可用账户
			
			//调用转帐接口,手费费由转出方出
			$result = transferBu($user_id,$t_user_id,$amt,$platformNo,$post_url,$transfer_id,1);
			
			if ($result['resp_code'] == '0000'){
				/*
				//承接人扣除转让费
				modify_account(array("money"=>-$amt),$t_user_id,"债:Z-".$transfer['load_id'].",承接金",16,1);
				//转让人接受转让费
				modify_account(array("money"=>$amt),$user_id,"债:Z-".$transfer['load_id'].",转让金",15,1);
				*/
							
				$sql = "update ".DB_PREFIX."deal_load_transfer set ips_status = 2, pMerBillNo = '".$t_user_id."',t_user_id = lock_user_id, transfer_time = '".get_gmtime()."', ips_bill_no = id where ips_status = 0 and id =".$transfer_id;
				//echo $sql;
				$GLOBALS['db']->query($sql);
				
				//将用户投资回款计划,收款人更改为：承接者
				$sql = "update ".DB_PREFIX."deal_load_repay set t_user_id = ".$t_user_id." where has_repay = 0 and load_id =".intval($transfer['load_id'])." and user_id =".intval($transfer['user_id'])." and deal_id = ".$deal_id;
					//echo $sql;
				$GLOBALS['db']->query($sql);
				
				//转让手续费
				if($fee!=0){
					$rs = transferBu($mchnt_user,$user_id,$fee,$platformNo,$post_url,$deal_id,1,17,"债:Z-".$transfer['load_id'].",转让管理费");
					/*if ($rs['resp_code'] == '0000'){
						modify_account(array("money"=>-$fee),$user_id,"债:Z-".$transfer['load_id'].",转让管理费",17,1);
						modify_account(array("money"=>$fee),$mchnt_user,"债:Z-".$transfer['load_id'].",转让管理费",17,1);
					}*/
				}
				$result['status'] =	1;
				$result['info'] = '转让成功';
			}else{
				//解除锁定
				$sql = "update ".DB_PREFIX."deal_load_transfer set lock_user_id = 0, lock_time = 0 where id =".$transfer_id;				
				$GLOBALS['db']->query($sql);
				$fuiou_code = fuiou_code();
				$result['info'] = '转帐失败code:'.$result['resp_code'].';msg:'.$fuiou_code[$str3Req['resp_code']];
			}
		}else{
			$result['info'] = '该债权转让已经被其它用户锁定';
		}	
		return ajax_return($result);	
	}	
	
?>